<html>
<head><meta charset="utf-8"><title>Check if drop is implemented · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html">Check if drop is implemented</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221259502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259502">(Dec 31 2020 at 00:47)</a>:</h4>
<p>Is there a way to test if Drop is implemented for a particular type? I know <code>needs_drop</code> exists but that would return True if any of its fields need drop aswell. I just want to check at a first-struct level. </p>
<p>eg I want to know C implements drop regardless of the fact that <code>a: A</code> within it needs drop, and that D doesn't implement drop even though <code>a: A</code> does need drop. </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">A</span><span class="p">;</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">B</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">C</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a</span>: <span class="nc">A</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="nc">B</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">D</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">a</span>: <span class="nc">A</span><span class="p">,</span><span class="w"> </span><span class="n">b</span>: <span class="nc">B</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="221259602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259602">(Dec 31 2020 at 00:49)</a>:</h4>
<p>The <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/traits/fn.type_implements_trait.html"><code>type_implements_trait</code></a> maybe sounds like what you need?</p>



<a name="221259830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259830">(Dec 31 2020 at 00:53)</a>:</h4>
<p>How do I pass the trait here? <span aria-label="open mouth" class="emoji emoji-1f62e" role="img" title="open mouth">:open_mouth:</span></p>



<a name="221259879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259879">(Dec 31 2020 at 00:54)</a>:</h4>
<p>I'll see how this is used</p>



<a name="221259934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259934">(Dec 31 2020 at 00:55)</a>:</h4>
<p>Ah the DefId is that of the trait itself</p>



<a name="221259938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259938">(Dec 31 2020 at 00:55)</a>:</h4>
<p>All I know for sure is that you can get the <code>DefId</code> for the <code>Drop</code> trait in <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/lang_items/struct.LanguageItems.html#method.drop_trait"><code>LanguageItems::drop_trait</code></a></p>



<a name="221259998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221259998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221259998">(Dec 31 2020 at 00:56)</a>:</h4>
<p>Thanks, I was just gonna go look for that <span aria-label="heart" class="emoji emoji-2764" role="img" title="heart">:heart:</span></p>



<a name="221260001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260001">(Dec 31 2020 at 00:56)</a>:</h4>
<p>(Which you can get from <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.lang_items"><code>TyCtxt::lang_items</code></a>)</p>



<a name="221260016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260016">(Dec 31 2020 at 00:57)</a>:</h4>
<p>Curios when would DefId for Drop not be available?</p>



<a name="221260072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260072">(Dec 31 2020 at 00:58)</a>:</h4>
<p>If the lang item is not defined, I guess</p>



<a name="221260090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260090">(Dec 31 2020 at 00:58)</a>:</h4>
<p>Looks like <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.require_lang_item"><code>require_lang_item</code></a> would handle the failure case for you</p>



<a name="221260100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260100">(Dec 31 2020 at 00:59)</a>:</h4>
<p>But depending on where you are in the compiler this might not be needed, I guess? I actually don't know <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="221260178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260178">(Dec 31 2020 at 01:00)</a>:</h4>
<p>I'm assuming if I have been able to safely call <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyS.html#method.needs_drop">needs_drop</a>, I should be fine</p>



<a name="221260206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260206">(Dec 31 2020 at 01:01)</a>:</h4>
<p>thanks for the help <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="221260367"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260367" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260367">(Dec 31 2020 at 01:04)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281950">Aman Arora</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Check.20if.20drop.20is.20implemented/near/221260178">said</a>:</p>
<blockquote>
<p>I'm assuming if I have been able to safely call <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyS.html#method.needs_drop">needs_drop</a>, I should be fine</p>
</blockquote>
<p>I <em>think</em> this is the kind of thing that 1) never appear in the wild (you'd need to have <code>#[no_core]</code> and stuff) and 2) if you're writing for an unstable feature you can leave a fixme and clean it up later. Just my uninformed opinion though</p>



<a name="221260484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Check%20if%20drop%20is%20implemented/near/221260484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Check.20if.20drop.20is.20implemented.html#221260484">(Dec 31 2020 at 01:07)</a>:</h4>
<p>Sounds good <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>